<script setup lang="ts">
import { computed, onMounted, ref, shallowRef, useTemplateRef, watch } from 'vue';
import { useBoolean } from '@sa/hooks';
import type { ImageStyle } from './utils/util';
import VerifySlide from './modules/verify-slide.vue';
import VerifyPoints from './modules/verify-points.vue';

interface Props {
  captchaType: 'blockPuzzle' | 'clickWord';
  mode?: string;
  vSpace?: number;
  explain?: string;
  imgSize?: ImageStyle;
  blockSize?: ImageStyle;
  barSize?: ImageStyle;
}

interface Emit {
  (e: 'success', data: any): void;
  (e: 'error'): void;
}

defineOptions({ name: 'ActionVerify' });

const props = withDefaults(defineProps<Props>(), {
  mode: undefined,
  vSpace: undefined,
  explain: undefined,
  imgSize: () => ({
    width: '310px',
    height: '155px'
  }),
  blockSize: undefined,
  barSize: undefined
});

const emit = defineEmits<Emit>();

defineExpose({
  show
});

const instance = useTemplateRef<any>('instance');

/** 是否点击显示验证码 */
const { bool: clickShow, setBool: setClickShow } = useBoolean();
/** 内部类型 */
const verifyType = ref();
/** 所用组件类型 */
const componentType = shallowRef();
/** 默认图片 */
const defaultImg = new URL(`@/assets/images/login/action-verify-default.jpg`, import.meta.url).href;

const showBox = computed(() => {
  if (props.mode === 'pop') {
    return clickShow.value;
  }
  return true;
});

onMounted(() => {
  uuid();
});

function uuid() {
  const s: string[] = [];
  const hexDigits = '0123456789abcdef';
  for (let i = 0; i < 36; i += 1) {
    if (i === 14) {
      s[i] = '4';
    } else if ([8, 13, 18, 23].includes(i)) {
      s[i] = '-';
    }
    const hex = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    if (i === 19) {
      s[i] = hexDigits.substr((hex && 0x3) || 0x8, 1);
    } else {
      s[i] = hex;
    }
  }

  const slider = `slider-${s.join('')}`;
  const point = `point-${s.join('')}`;
  /** 判断下是否存在 slider */
  if (!localStorage.getItem('slider')) {
    localStorage.setItem('slider', slider);
  }
  if (!localStorage.getItem('point')) {
    localStorage.setItem('point', point);
  }
}

function refresh() {
  if (instance.value.refresh) {
    instance.value.refresh();
  }
}
function closeBox() {
  setClickShow(false);
  refresh();
}
function show() {
  if (props.mode === 'pop') {
    setClickShow(true);
  }
}

watch(
  () => props.captchaType,
  () => {
    const componentMap = {
      blockPuzzle: {
        verifyType: '2',
        componentType: VerifySlide
      },
      clickWord: {
        verifyType: '',
        componentType: VerifyPoints
      }
    };
    verifyType.value = componentMap[props.captchaType].verifyType;
    componentType.value = componentMap[props.captchaType].componentType;
  },
  { immediate: true }
);
</script>

<template>
  <Transition name="fade-slide" mode="out-in" appear>
    <div v-show="showBox" :class="mode === 'pop' ? 'mask' : ''">
      <div :class="mode == 'pop' ? 'verifybox' : ''" :style="{ 'max-width': parseInt(imgSize.width) + 30 + 'px' }">
        <div v-if="mode == 'pop'" class="verifybox-top">
          请完成安全验证
          <SvgIcon local-icon="material-symbols-close-rounded" class="verifybox-close" @click="closeBox"></SvgIcon>
        </div>
        <div class="verifybox-bottom" :style="{ padding: mode == 'pop' ? '15px' : '0' }">
          <!-- 验证码容器 -->
          <component
            :is="componentType"
            v-if="componentType"
            ref="instance"
            :captcha-type="captchaType"
            :type="verifyType"
            :mode="mode"
            :v-space="vSpace"
            :explain="explain"
            :img-size="imgSize"
            :block-size="blockSize"
            :bar-size="barSize"
            :default-img="defaultImg"
            @show="show"
            @set-click-show="setClickShow"
            @close-box="closeBox"
            @success="emit('success', $event)"
            @error="emit('error')"
          />
        </div>
      </div>
    </div>
  </Transition>
</template>

<style lang="scss">
.verifybox {
  position: relative;
  box-sizing: border-box;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 6px;
}
.verifybox-top {
  padding: 0 15px;
  height: 50px;
  line-height: 50px;
  text-align: left;
  font-size: 16px;
  color: #45494c;
  border-bottom: 1px solid #e4e7eb;
  box-sizing: border-box;
}
.verifybox-bottom {
  padding: 15px;
  box-sizing: border-box;
}
.verifybox-close {
  position: absolute;
  top: 13px;
  right: 9px;
  width: 24px;
  height: 24px;
  text-align: center;
  cursor: pointer;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1001;
  width: 100%;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  /* display: none; */
  transition: all 0.5s;
}
.verify-tips {
  position: absolute;
  left: 0px;
  bottom: 0px;
  width: 100%;
  height: 30px;
  line-height: 30px;
  color: #fff;
  padding-left: 12px;
  font-size: 14px;
  box-sizing: border-box;
}
.suc-bg {
  background-color: rgba(92, 184, 92, 0.5);
  filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#7f5CB85C, endcolorstr=#7f5CB85C);
}
.err-bg {
  background-color: rgba(217, 83, 79, 0.5);
  filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#7fD9534F, endcolorstr=#7fD9534F);
}
.tips-enter,
.tips-leave-to {
  bottom: -38px;
}
.tips-enter-active,
.tips-leave-active {
  transition: bottom 0.5s;
}
/* ---------------------------- */
/*常规验证码*/
.verify-code {
  font-size: 20px;
  text-align: center;
  cursor: pointer;
  margin-bottom: 5px;
  border: 1px solid #ddd;
}

.cerify-code-panel {
  height: 100%;
  overflow: hidden;
}

.verify-code-area {
  float: left;
}

.verify-input-area {
  float: left;
  width: 60%;
  padding-right: 10px;
}

.verify-change-area {
  line-height: 30px;
  float: left;
}

.varify-input-code {
  display: inline-block;
  width: 100%;
  height: 25px;
}

.verify-change-code {
  color: #1e80ff;
  cursor: pointer;
}

.verify-btn {
  width: 200px;
  height: 30px;
  background-color: #1e80ff;
  color: #ffffff;
  border: none;
  margin-top: 10px;
}

/*滑动验证码*/
.verify-bar-area {
  position: relative;
  background: #ffffff;
  text-align: center;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  border: 1px solid rgb(224, 224, 230);
}

.verify-bar-area .verify-move-block {
  position: absolute;
  top: 0px;
  left: 0;
  background: #fff;
  cursor: pointer;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  box-shadow: 2px 0 8px 0 rgb(29, 35, 41, 0.05);
  transition: background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.verify-bar-area .verify-move-block:hover {
  background-color: #1e80ff;
  color: #ffffff;
}

.verify-bar-area .verify-left-bar {
  position: absolute;
  top: -1px;
  left: -1px;
  background: #ecf8ff;
  cursor: pointer;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  border: 1px solid #ddd;
}

.verify-img-panel {
  margin: 0;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  border-radius: 3px;
  position: relative;
  overflow: hidden;
}

.verify-img-panel .verify-refresh {
  width: 25px;
  height: 25px;
  text-align: center;
  padding: 5px;
  cursor: pointer;
  position: absolute;
  top: 0;
  right: 0;
  z-index: 2;
}

.verify-img-panel .icon-refresh {
  font-size: 20px;
  color: #fff;
  &.rotating {
    display: inline-block;
    animation: loadingRefresh 1.5s linear infinite;
  }
}

@keyframes loadingRefresh {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(1turn);
  }
}

.verify-img-panel .verify-gap {
  background-color: #fff;
  position: relative;
  z-index: 2;
  border: 1px solid #fff;
}

.verify-bar-area .verify-move-block .verify-sub-block {
  position: absolute;
  text-align: center;
  z-index: 3;
  /* border: 1px solid #fff; */
}

.verify-bar-area .verify-move-block .verify-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 18px;
}

.verify-bar-area .verify-msg {
  z-index: 3;
  color: rgb(51, 54, 57);
  font-size: 14px;
}

.iconfont {
  font-family: 'iconfont' !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-check:before {
  content: '\e6da'; // .el-icon-check
  font-family: element-icons !important;
  display: block;
  font-size: 16px;
}

.icon-close:before {
  content: '\e6db';
  font-family: element-icons !important;
  display: block;
  font-size: 18px;
  line-height: 24px;
}

.icon-close2:before {
  content: '\e6db';
  font-family: element-icons !important;
  display: block;
  font-size: 18px;
  line-height: 24px;
}

.icon-right:before {
  content: '\e6e0';
  font-family: element-icons !important;
  font-size: 18px;
}

.icon-refresh:before {
  content: '\e6c8';
  font-family: element-icons !important;
  display: inline-block;
  line-height: 1;
}
.defaultImg {
  display: block;
  width: 100%;
  height: 100%;
  background-position: center center;
  background-size: cover;
}
</style>
